<?php
/**
 * 业主设置
 * @author Administrator
 *
 */
class setting extends CControllerOwnerLoginBase{
	
	/**
	 * 账户使用交易密码，开启/关闭扣减积分功能设置
	 */
	public function pay()
	{
		$osModel = new ownerShopModel();
		
		$osInfo = $osModel->selectByWhere("t_owner_shop","count(0) as c,sum(decode(owner_card_status,1,1,0)) as d",array(),"del_flag=0 and owner_id=".$this->_ownerID,"","owner_id");

		$ownerInfo = $osModel->selectByWhere("t_owner","use_pay_pwd",array(),"owner_id=".$this->_ownerID);
		
		$use_pay_pwd = $ownerInfo["use_pay_pwd"];

		$type=0;//没开通过
		if($osInfo)
		{
			if($osInfo["c"] == $osInfo["d"])
			{
				$type = 1;//全部开通
			}
			else if($osInfo["d"]==0){
				$type = 2; //全部关闭
			}
			else{
				$type = 3; //部分开通
			}
		}
		$this->render("setting/pay",array("type"=>$type,"use_pay_pwd"=>$use_pay_pwd));
	}
	 
	/**
	 * 修改所有商户的扣减积分状态
	 */
	public function changeStatus()
	{
		$type=$this->getRequestData("type");
		$pwd=$this->getRequestData("pwd");
		
		if($type!=1 && $type!=2)
		{
			echo -10;
			exit;
		}
		
		$pwd = CCommon::pwdMd5($pwd);
		
		$ownerModel = new ownerModel();
		if($ownerModel->checkPayPwdByOwner($this->_ownerID,$pwd))
		{
			if($ownerModel->changeCardStatusForAllShop($this->_ownerID,$type))
			{
				echo 1;
			}
			else{
				echo -2; //修改失败
			}
		}
		else{
			echo -1; //密码不正确
		}
		
		exit;
	}
	
	/**
	 * 修改使用账户密码功能
	 */
	public function changeUsePayPwd()
	{
		$type=$this->getRequestData("type");
		$pwd=$this->getRequestData("pwd");
		
		if($type!=1 && $type!=0)
		{
			echo -10;
			exit;
		}
		
		$pwd = CCommon::pwdMd5($pwd);
		
		$ownerModel = new ownerModel();
		if($ownerModel->checkPayPwdByOwner($this->_ownerID,$pwd))
		{
			$ownerInfo["owner_id"] = $this->_ownerID;
			$ownerInfo["use_pay_pwd"] = $type;
			
			if($ownerModel->updateByPkId($ownerInfo))
			{
				echo 1;
			}
			else{
				echo -2;
			}
		}
		else{
			echo -1; //密码不正确
		}
		
		exit;
	}
	
	/**
	 * 用户基本设置
	 */
	public function user()
	{
		$rule = CCustomFormValidator::ownerEditValidator();
		$this->formValidator->init(array("formID" => "userForm", "alertErrorMsg" => false));
		$this->formValidator->setRules($rule);
		
		$oModel = new ownerModel();
		if ($_POST) {
			$error = $this->formValidator->run();
			if ($error) {
				echo "error";
				exit;
			}
			else {
				$ownerInfo["owner_id"] = $this->_ownerID;
				$ownerInfo["mail"] = $this->getRequestData("txt_mail");
				$ownerInfo["phone"] = $this->getRequestData("txt_phone");
				$ownerInfo["address"] = $this->getRequestData("txt_address");
				
				if($oModel->updateByPkId($ownerInfo))
				{
					$this->setJsBlock("alert('保存成功');");
				}
			}
		}
		
		$oInfo = $oModel->selectByWhere("t_owner",
				"mail,phone,address,user_name,owner_name,main_card,supplementary_card",
				array(),"del_flag=0 and owner_id=".$this->_ownerID);
		
		$oInfo = array_merge($oInfo,$this->_ownerInfo);

		$this->render("setting/user",array("ownerInfo"=>$oInfo));
	}
}